package com.alcatellucent.ldap;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.alcatellucent.R;
import com.alcatellucent.app.LDAPApp;
import com.alcatellucent.ldap.service.LdapCompanyService;
import com.alcatellucent.ldap.util.LdapCompanyAdapter;
import com.alcatellucent.vo.CompanyVO;
import com.alcatellucent.vo.UserVO;
import com.unboundid.ldap.sdk.LDAPConnection;

public class CompanyActivity extends ExpandableListActivity {

	private ExpandableListView companyView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.companylayout);
		getCompanyList();
	}

	private void getCompanyList() {

		LdapCompanyService service = new LdapCompanyService();
		LDAPConnection connection = ((LDAPApp) getApplicationContext())
				.getConnection();
		if (connection != null) {
			final ArrayList<CompanyVO> companyList = service
					.getCompanyList(connection);
			LdapCompanyAdapter companyAdapter = null;
			if (companyList != null) {
				// departmentView = (ExpandableListView)
				// findViewById(R.id.expandableListView1);
				companyView = getExpandableListView();
				companyAdapter = new LdapCompanyAdapter(
						getApplicationContext(), companyList);
				companyView.setAdapter(companyAdapter);

				companyView.setOnChildClickListener(new OnChildClickListener() {

					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {

						Intent userInfoIntent = new Intent(
								getApplicationContext(),
								UserDetailedInfoActivty.class);

						UserVO userVO = companyList.get(groupPosition)
								.getUserList().get(childPosition);

						userInfoIntent.putExtra("userdetailinfo", userVO);

						startActivity(userInfoIntent);
						return false;
					}
				});
			}
		}
	}

}
